package com.yuantu.judiciary.model.word;

import lombok.Data;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;

/**
 * @author kgwei
 * 表格的列
 */
@Data
public class WordTableCell {

    private String text;

    /**
     * @see STVerticalJc
     * STVerticalJc.CENTER
     */
    private STVerticalJc.Enum vAlign;

    /**
     * A7BFDE
     */
    private String fillColor;

    private Integer fontSize;

    private String fontFamily;

    private Boolean bold;

    private Integer widthPct;

    private STTblWidth.Enum withType;

    /**
     * @see ParagraphAlignment
     * ParagraphAlignment.CENTER
     */
    private ParagraphAlignment alignment;

    public static WordTableCell defaultTitleCell(String text) {
        WordTableCell wordTableCell = new WordTableCell();
        wordTableCell.setText(text);
        wordTableCell.setFontSize(WordDefaultValue.TABLE_TITLE_FONT_SIZE);
        wordTableCell.setAlignment(ParagraphAlignment.CENTER);
        wordTableCell.setVAlign(STVerticalJc.CENTER);
        wordTableCell.setFillColor(WordDefaultValue.TABLE_TITLE_BG_COLOR);
        return wordTableCell;
    }

    public void cellBuild(XWPFTableCell cell) {
        if (vAlign != null || fillColor != null || widthPct != null) {
            CTTcPr tcpr = cell.getCTTc().addNewTcPr();
            if (vAlign != null) {
                CTVerticalJc va = tcpr.addNewVAlign();
                va.setVal(vAlign);
            }
            if (widthPct != null) {
                CTTblWidth cellWidth = tcpr.addNewTcW();
                cellWidth.setW(BigInteger.valueOf(widthPct));
                if (withType != null) {
                    cellWidth.setType(withType);
                }
            }
            CTShd ctshd = tcpr.addNewShd();
            ctshd.setColor("auto");
            ctshd.setVal(STShd.CLEAR);
            if (fillColor != null) {
                ctshd.setFill(fillColor);
            }
        }
        XWPFParagraph para = cell.getParagraphs().get(0);
        XWPFRun rh = para.createRun();
        rh.setText(text);
        if (bold != null) {
            rh.setBold(bold);
        }
        if (fontSize != null) {
            rh.setFontSize(fontSize);
        }
        if (fontFamily != null) {
            rh.setFontFamily(fontFamily);
        }
        if (alignment != null) {
            para.setAlignment(alignment);
        }
    }
}
